<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'data'.DS.'DAORoute.php');
require_once('FTConnection.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class FTDAORoute implements DAORoute{
	
	private $_connection = null;
	private $_routeFTID;
	
	public function __construct(){
		$this->_connection=FTConnection::getInstance();
		$this->getRouteFTId();
	}
	
	private function getRouteFTId(){
		$daoRouteConfig=JDAOFactory::getInstance()->getDAOFTRouteConfig();
		$this->_routeFTID=$daoRouteConfig->getFTRouteConfig();
	}
	
	public function getRoute($id){
		$row=$this->_connection->query("SELECT * FROM ".$this->_routeFTID->getRouteFTId()." WHERE id=".$id);
		return DTOFactory::getInstance()->getDTORouteFromArray($row[0]);
	}
	
	public function getRouteDesc($id){
		$row=$this->_connection->query("SELECT id,userID,name,description,distance FROM ".$this->_routeFTID->getRouteFTId()." WHERE id=".$id);
		return DTOFactory::getInstance()->getDTORouteDescFromArray($row[0]);
	}
	
	public function getUserCreatedRoutes($user){
		$rows=$this->_connection->query("SELECT id,userID,name,description,distance FROM ".$this->_routeFTID->getRouteFTId()." WHERE userID=".$user);
		return DTOFactory::getInstance()->getDTORoutesDescFromArray($rows);
	}
	
	public function getNearestRoutes($location,$circleRadius,$maxDistance){
		$condDistance=$this->getCondDistance($maxDistance);
		$nearestRoutes=null;
		if($location){
			$where=null;
			if($condDistance){
				$where=" AND ".$condDistance;
			}
			//print_r("SELECT ROWID,userID,name,description,distance FROM ".$this->_routeFTID->getRouteFTId()." WHERE ST_INTERSECTS(route,CIRCLE(LATLNG(".$location->getLat().", ".$location->getLng()."), ".$circleRadius.")) ".$where);
			$nearestRoutes=$this->_connection->query("SELECT * FROM ".$this->_routeFTID->getRouteFTId()." WHERE ST_INTERSECTS(route,CIRCLE(LATLNG(".$location->getLat().", ".$location->getLng()."), ".$circleRadius.")) ".$where);
			//print_r($nearestRoutes);
		}
		return DTOFactory::getInstance()->getDTORoutesDescFromArray($nearestRoutes);
	}
	
		public function getNearestRoutesSimilar($location,$circleRadius,$maxDistance,$minDistance){
		$condDistance=$this->getCondDistanceSimilar($maxDistance, $minDistance);
		$nearestRoutes=null;
		if($location){
			$where=null;
			if($condDistance){
				$where=" AND ".$condDistance;
			}
			//print_r("SELECT ROWID,userID,name,description,distance FROM ".$this->_routeFTID->getRouteFTId()." WHERE ST_INTERSECTS(route,CIRCLE(LATLNG(".$location->getLat().", ".$location->getLng()."), ".$circleRadius.")) ".$where);
			$nearestRoutes=$this->_connection->query("SELECT * FROM ".$this->_routeFTID->getRouteFTId()." WHERE ST_INTERSECTS(route,CIRCLE(LATLNG(".$location->getLat().", ".$location->getLng()."), ".$circleRadius.")) ".$where);
			//print_r($nearestRoutes);
		}
		return DTOFactory::getInstance()->getDTORoutesDescFromArray($nearestRoutes);
	}
	
	private function getWheres($keywords){
		//Create an array with where conditions based on keywords
		$keywords=trim($keywords);
		$words = explode( ' ', $keywords );
        $wheres = array();
        foreach ($words as $word){
        	$wheres2 = array();
        	$wheres2[] = "name CONTAINS IGNORING CASE '".addslashes($word)."'";
        	$wheres[] = implode( '', $wheres2 );
        }
        return $wheres;
	}
	
	private function getCondDistance($maxDistance){
		//Set the condition if maxDistance is bigger than 0
		$condDistance = null;
		if($maxDistance && is_numeric($maxDistance)){
			$condDistance="distance<=".$maxDistance;
		}
		return $condDistance;
	}
	
		
	private function getCondDistanceSimilar($maxDistance,$minDistance){
		//Set the condition if maxDistance is bigger than 0
		$condDistance = null;
		if($maxDistance && is_numeric($maxDistance)){
			$condDistance="distance<=".$maxDistance;
			$condDistance=$condDistance." and distance>=".$minDistance;
		}
		return $condDistance;
	}
	
	private function getRoutesWithWheres($wheres,$condDistance){
		$results=array();
		foreach($wheres as $where){
			if($condDistance){
				$where=$where." AND ".$condDistance;
			}
			$result=$this->_connection->query("SELECT * FROM ".$this->_routeFTID->getRouteFTId()." WHERE ".$where);
			foreach($result as $route){
				$results[]=$route;
			}
		} 
		return $results;
	}
	
	public function getRoutesDescByKeywords($keywords,$maxDistance){
		if($keywords!='' && $keywords!=null){
			$wheres=$this->getWheres($keywords);	
			$condDistance=$this->getCondDistance($maxDistance);
			$rows=$this->getRoutesWithWheres($wheres,$condDistance);
			return DTOFactory::getInstance()->getDTORoutesDescFromArray($rows);	
		}
		return array();
	}
	
	public function getRoutesByDistance($maxDistance){
		$condDistance=$this->getCondDistance($maxDistance);
		$where='';
		if($condDistance){
			$where=" WHERE ".$condDistance;
		}
		$rows=$this->_connection->query("SELECT * FROM ".$this->_routeFTID->getRouteFTId().$where);
		return DTOFactory::getInstance()->getDTORoutesDescFromArray($rows);
	}
	
	public function saveRoute($route){
		 		      
								  
		return $this->_connection->query("INSERT INTO ".$this->_routeFTID->getRouteFTId().
								  " (id,userID,name,description,route,distance,elevation) VALUES (".
								  $route->getId().",".
								  $route->getUser()->get('id').",'".
								  $route->getName()."','".
								  $route->getDescription()."','".
								  $route->getRawCoordinates()."',".
								  $route->getDistance().",'".
								  $route->getElevation()."')");
	}
}



?>